package com.power.dao.impl;

import com.power.dao.TowerDao;
import com.power.ov.ResultData;
import com.power.ov.ResultDataTable;
import com.power.pojo.Tower;
import com.power.utils.JdbcBuild;

import java.sql.SQLException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 李辽
 * Date: 2020/10/03 17:12
 * Description:
 * Version: V1.0
 */
public class TowerDaoImpl extends JdbcBuild implements TowerDao {
    @Override
    public ResultDataTable queryListPower(int page, int limit) throws SQLException {
        String sql = "SELECT towper_id,tower_name,line_number,state_name FROM tower AS t,line AS l,states AS s WHERE l.line_id=t.line_id AND t.tower_state=s.state_id LIMIT ?,?";
        int param1 = (page-1)*limit;
        int param2 = limit;
        Object[] params = {param1,param2};
        List<Tower> towerList = super.queryListEntity(Tower.class,sql,params);
        ResultDataTable resultDataTable = new ResultDataTable();
        resultDataTable.setCode(0);
        resultDataTable.setCount(queryCount());
        resultDataTable.setData(towerList);
        return resultDataTable;
    }

    private long queryCount() throws SQLException {
        String sql = "SELECT count(*) FROM tower AS t,line AS l,states AS s WHERE l.line_id=t.line_id AND t.tower_state=s.state_id ";
        long result = super.findAllForInt(sql,null);
        return result;
    }

    @Override
    public int DeleteTowerByName(int towper_id) throws SQLException {
        String sql = "DELETE FROM tower WHERE towper_id = ?";
        int result = super.executeUpdate(sql,towper_id);
        System.out.println("进入删除后台dao层"+result);
        return result;
    }

    @Override
    public int ChangeTower(Tower tower1) throws SQLException {
        System.out.println("进入后台dao层，数据为"+tower1.toString());
        String sql = "UPDATE tower SET tower_name = ?,tower_state = ?,line_id=? WHERE towper_id = ?; ";
        Object[] params = {tower1.getTower_name(),tower1.getState_id(),tower1.getLine_number(),tower1.getTowper_id()};
        System.out.println(tower1.getTowper_id());
        int result = super.executeUpdate(sql, params);
        System.out.println("进入杆塔编辑后台dao层");
        return result;
    }

    @Override
    public List<Tower> findTowerById(int towper_id){
        String sql = "select * from tower where tower_id=?";
        List<Tower> towerList = null;
        try {
            towerList = super.queryListEntity(Tower.class,sql,towper_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return towerList;
    }

    @Override
    public int Addtopwer(Tower tower1) throws SQLException {
        String sql = "INSERT INTO tower(tower_name,tower_state,line_id) VALUES(?,?,?)";
        Object[] params = {tower1.getTower_name(),tower1.getTower_state(),tower1.getLine_number()};
        int result = super.executeUpdate(sql, params);
        System.out.println("到达后台测试dao");
        return result;
    }

    @Override
    public ResultDataTable SearchTowerByLineId(Tower tower) throws SQLException {
        String sql = "SELECT * FROM tower WHERE line_id = ? AND tower_state = ?";
        Object[] params = {tower.getLine_number(),tower.getState_name()};

        ResultDataTable resultDataTable = new ResultDataTable();
        List<Tower> towerList = super.queryListEntity(Tower.class,sql,params);
        resultDataTable.setData(towerList);
        resultDataTable.setCode(200);
        resultDataTable.setMessage("查询成功");
        resultDataTable.setCount(excutcount(tower));
        return resultDataTable;
    }
    private long excutcount(Tower tower) throws SQLException {
        String sql = "SELECT count(*) FROM tower WHERE line_id = ? AND tower_state = ?";
        Object[] params = {tower.getLine_number(),tower.getState_name()};
        return  super.findAllForInt(sql,params);
    }

    @Override
    public ResultDataTable SearchTower(int page, int limit, int line_id, int tower_state) throws SQLException {
        String sql = "SELECT towper_id,tower_name,line_number,state_name FROM tower AS t,line AS l,states AS s WHERE l.line_id=t.line_id AND t.tower_state=s.state_id AND t.line_id=? AND t.tower_state = ? limit ?,?";
        int param1 = (page-1)*limit;
        int param2 = limit;
        Object[] params = {line_id,tower_state,param1,param2};
        List<Tower> towerList = super.queryListEntity(Tower.class,sql,params);

        ResultDataTable resultDataTable = new ResultDataTable();
        resultDataTable.setData(towerList);
        resultDataTable.setMessage("查询成功");
        resultDataTable.setCode(0);
        resultDataTable.setCount(QueryCountBySearch(params));

        System.out.println("Dao  所属线路编号"+line_id+"   "+"杆塔状态"+tower_state +"数据"+resultDataTable);
        return resultDataTable;
    }
    private long QueryCountBySearch(Object[] params) throws SQLException {
        String sql = "SELECT towper_id,tower_name,line_number,state_name FROM tower AS t,line AS l,states AS s WHERE l.line_id=t.line_id AND t.tower_state=s.state_id AND t.line_id=? AND t.tower_state = ? limit ?,?";
        long result = super.findAllForInt(sql,params);
        return result;
    }
    @Override
    public int queryLineIdByIneNumber(String line_number) throws SQLException {
        String sql = "SELECT line_id FROM line WHERE line_number = ?";
        Object[] params = {line_number};
        Tower tower = queryEntity(Tower.class, sql, params);
        int result = tower.getLine_id();
        return result;
    }

    @Override
    public ResultData DefectSelect(String line_id) throws SQLException {
        String sql = "SELECT * FROM tower WHERE line_id =?";
        List<Tower> towerList = super.queryListEntity(Tower.class, sql, line_id);
        ResultData resultData = new ResultData();
        resultData .setData(towerList);
        resultData.setCode(0);
        resultData.setMsg("查询成功！！！");
        return resultData;
    }

    @Override
    public int ModifiesTowerStatus(int towper_id, int newState) throws SQLException {
        String sql = "UPDATE tower SET tower_state = ? WHERE towper_id = ?;";
        Object[] params = {newState,towper_id};
        int result = super.executeUpdate(sql,params);
        return result;
    }




}
